package cz.muni.fi.pa165.sme.web.panels.burnedCalories;

import cz.muni.fi.pa165.sme.api.BurnedCaloriesService;
import cz.muni.fi.pa165.sme.api.SportActivityService;
import cz.muni.fi.pa165.sme.api.to.BurnedCaloriesTO;
import cz.muni.fi.pa165.sme.web.common.ErrorBehavior;
import cz.muni.fi.pa165.sme.web.converters.ConvertUtils;
import cz.muni.fi.pa165.sme.web.fo.BurnedCaloriesFO;
import cz.muni.fi.pa165.sme.web.fo.SportActivityFO;
import cz.muni.fi.pa165.sme.web.pages.burnedCalories.CaloriesListPage;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.NumberTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.apache.wicket.validation.validator.MinimumValidator;
import org.springframework.core.convert.ConversionService;

/**
 * @author jirankova
 */
public class CaloriesForm extends Panel {

	@SpringBean(name = "conversionServiceFe")
	private ConversionService conversionService;

	@SpringBean
	private SportActivityService sportActivityService;

	@SpringBean
	private BurnedCaloriesService burnedCaloriesService;

	public CaloriesForm(final String id, final IModel<BurnedCaloriesFO> model) {
		super(id, model);

		//definition of form components
		final FormComponent<SportActivityFO> sportActivities = new DropDownChoice<SportActivityFO>("sportActivity",
				ConvertUtils.convertList(sportActivityService.findAllSportActivities(), conversionService,
						SportActivityFO.class), new ChoiceRenderer<SportActivityFO>("title", "id"));
		sportActivities.setRequired(true);

		final FormComponent<Long> calories = new NumberTextField<Long>("calories").add(new MinimumValidator<Long>(0L));
		calories.setRequired(true);
		
		final FormComponent<Float> weight = new NumberTextField<Float>("weight").add(new MinimumValidator<Float>(0f));
		
		final FormComponent<Float> speed = new NumberTextField<Float>("speed").add(new MinimumValidator<Float>(0f));

		//form itself
		final Form<BurnedCaloriesFO> form = new Form<BurnedCaloriesFO>("caloriesForm", model) {
			@Override
			protected void onSubmit() {
				super.onSubmit();
				final BurnedCaloriesFO burnedCaloriesFO = model.getObject();

				final BurnedCaloriesTO burnedCaloriesTO = conversionService.convert(burnedCaloriesFO, BurnedCaloriesTO.class);

				if(burnedCaloriesService.exists(burnedCaloriesTO)) {
					burnedCaloriesService.updateBurnedCalories(burnedCaloriesTO);
				} else {
					burnedCaloriesService.createBurnedCalories(burnedCaloriesTO);
				}
				setResponsePage(CaloriesListPage.class);
			}
		};

		form.add(sportActivities);
		form.add(calories);
		form.add(weight);
		form.add(speed);

		//add form to the panel
		add(form);

		form.visitChildren(FormComponent.class, new IVisitor<FormComponent<?>, IVisit<Void>>() {
			@Override
			public void component(final FormComponent<?> object, final IVisit<IVisit<Void>> visit) {
				object.add(new ErrorBehavior());
				visit.dontGoDeeper();
			}
		});

		final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
		feedbackPanel.setOutputMarkupId(true);
		add(feedbackPanel);
	}
}
